package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.domain.User;
import com.example.demo.domain.dto.EditUserDTO;
import com.example.demo.domain.dto.RegisterDTO;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author lqk21
 * @description 针对表【user(用户表)】的数据库操作Service实现
 * @createDate 2025-05-30 21:44:05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    public UserMapper userMapper;


    @Override
    public boolean register(RegisterDTO registerDTO) {

        if (Objects.nonNull(userMapper.selectOne(
                new LambdaQueryWrapper<User>().eq(
                        User::getUsername, registerDTO.getUsername()
                )
        ))) {
            return false;
        }

        User user = new User();
        user.setUsername(registerDTO.getUsername());
        user.setPassword(registerDTO.getPassword());
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        int row = userMapper.insert(user);

        return row > 0;
    }

    @Override
    public Integer login(String username, String password) {
        User user = userMapper.selectOne(
                new LambdaQueryWrapper<User>().eq(
                        User::getUsername, username
                )
        );
        if (user == null) {
            return null;
        }
        if (!user.getPassword().equals(password)) {
            return null;
        }
        return user.getId();
    }

    @Override
    public boolean EditUserInfo( EditUserDTO editUser) {
        User user = userMapper.selectById(editUser.getId());
        user.setUsername(editUser.getUsername());
        user.setPassword(editUser.getPassword());
        user.setUpdateTime(LocalDateTime.now());
        int row = userMapper.updateById(user);
        return row > 0;
    }

}




